package com.jingan.nursinghome.regulatory.dialog.micrometertable;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Topicinfo;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.MyTextView;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/17.
 */

public class XuantiDialog2 extends BaseDialog {
    private XuantiDialog2(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_micrometertablleos_xuanti, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.crown_window_right);
        initUI();
        //adapterUI();
    }

    public static XuantiDialog2 xuantiDialog;

    public static BaseListenner<Integer> mBaseListenner;

    public static XuantiDialog2 getInstance(BaseActivity baseActivity, List<Topicinfo> topics,int position, BaseListenner<Integer> baseListenner) {
        mBaseListenner = baseListenner;
        padeIndex=position;
        topicinfos=topics;
        if (isfinish(baseActivity, xuantiDialog)) {
            xuantiDialog = new XuantiDialog2(baseActivity);
        }
        return xuantiDialog;
    }

    MyActionBar myActionBar;
    MyTextView mytextview;
    RecyclerView recyclerView;

    LinearLayoutManager linearLayoutManager;
    public void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "千分评分系统");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        mytextview = (MyTextView) findViewById(R.id.mytextview);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                outRect.left = ProportionUtils.getInstance().adapterInt(18);
                outRect.right = outRect.left;
                outRect.bottom = ProportionUtils.getInstance().adapterInt(21);
                if (itemPosition <= 4) {
                    outRect.top = outRect.bottom;
                }
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
//                outRect.left = ProportionUtils.getInstance().adapterInt(18);
//                outRect.right = outRect.left;
//                outRect.bottom = ProportionUtils.getInstance().adapterInt(21);

//                int postion = parent.getChildAdapterPosition(view);
//                if (postion <= 4) {
//                    outRect.top = ProportionUtils.getInstance().adapterInt(60);
//                }
//                if (postion % 5 == 0) {
//                    outRect.left = ProportionUtils.getInstance().adapterInt(30);
//                }
//                if ((postion + 1) / 5 == 0) {
//                    outRect.right = ProportionUtils.getInstance().adapterInt(30);
//                }
            }
        });
//        recyclerView.setAdapter(new PageAdapter(MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid,CreateMicrometerTableDailog
//                .jlyName).size(), MicrometerTableOSDialog.count, MicrometerTableOSDialog.getMaxPage()));
    }

    public void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.bg_bottom));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.view_line));
    }

    static int padeIndex=0;//当前选中页
    static List<Topicinfo> topicinfos;
    int select1=0;
    int select2=0;

    @Override
    protected void listener() {
        MicrometerTable.getInstance().getTopicinfoList().size();
//        padeIndex=MicrometerTableOSDialog.pageIndex;
        mytextview.setIndexAndMax( padeIndex+ "", topicinfos.size() + "");
//        recyclerView.getAdapter().notifyDataSetChanged();
//        List<Topicinfo> topicinfos = MicrometerTable.getInstance().getTopicinfoList();
        List<List<Topicinfo>> top2 = new ArrayList<>();
        List<Topicinfo> list = new ArrayList<>();
        for (int i = 0; i < topicinfos.size(); i++) {

            if (topicinfos.get(i).getSubjectcategory_name() != null) {
                if (list.size() > 0) {
                    top2.add(list);
                }
                list = new ArrayList<>();
            }
            topicinfos.get(i).setPosition(i+1);//题目下标从1开始
            list.add(topicinfos.get(i));

            if (i == (topicinfos.size() - 1)) {
                if (list.size() > 0) {
                    top2.add(list);
                }
            }
            if(padeIndex==i){
                select1=top2.size();
                select2=list.size();
            }
        }
        recyclerView.setAdapter(new PageAdapter2(top2));
        linearLayoutManager.scrollToPositionWithOffset(select1,0);
    }

    @Override
    protected void recycleView() {

    }


    class PageAdapter2 extends RecyclerView.Adapter<XuantiDialog2.PageAdapter2.MyViewHolder> {

        List<List<Topicinfo>> top2 = new ArrayList<>();

        public PageAdapter2(List<List<Topicinfo>> top2) {
            this.top2 = top2;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView title;
            RecyclerView recyclerView;

            public MyViewHolder(View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title);
                recyclerView = itemView.findViewById(R.id.recyclerView);
            }
        }


        @Override
        public XuantiDialog2.PageAdapter2.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_xuanti2, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。

            return new XuantiDialog2.PageAdapter2.MyViewHolder(view);
            //return new MyViewHolder(view);
        }

        List<Integer> index = new ArrayList<>();

        @Override
        public void onBindViewHolder(XuantiDialog2.PageAdapter2.MyViewHolder holder, final int position) {
            holder.title.setText(top2.get(position).get(0).getSubjectcategory_name());//分类

            GridLayoutManager gridLayoutManager = new GridLayoutManager(baseActivity, 5);
            holder.recyclerView.setLayoutManager(gridLayoutManager);
            holder.recyclerView.setAdapter(new PageAdapter3(top2.get(position)));
            gridLayoutManager.scrollToPositionWithOffset(select2,0);

        }

        @Override
        public int getItemCount() {
            return top2.size();
        }
    }

    class PageAdapter3 extends RecyclerView.Adapter<XuantiDialog2.PageAdapter3.MyViewHolder> {

        List<Topicinfo> top3 = new ArrayList<>();

        public PageAdapter3(List<Topicinfo> top3) {
            this.top3 = top3;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            cn.android.support.v7.lib.sin.crown.kotlin.widget.RoundTextView title;

            public MyViewHolder(View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title);
                recyclerView = itemView.findViewById(R.id.recyclerView);
            }
        }


        @Override
        public XuantiDialog2.PageAdapter3.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_xuanti3, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。

            return new XuantiDialog2.PageAdapter3.MyViewHolder(view);
            //return new MyViewHolder(view);
        }


        @Override
        public void onBindViewHolder(XuantiDialog2.PageAdapter3.MyViewHolder holder, final int position) {
            holder.title.setText(top3.get(position).getYid());
            String p = top3.get(position).getPscore();
            if (p != null && !p.trim().equals("")) {
                //已答题
                holder.title.setTextColor(Color.parseColor("#FFFFFF"));
                holder.title.setBackgroundColor(Color.parseColor("#00d8ff"));
            } else {
                //未答题
                holder.title.setTextColor(Color.parseColor("#FFFFFF"));
                holder.title.setBackgroundColor(Color.parseColor("#fc3778"));
            }
            //当前选中页
            if(top3.get(position).getPosition()==padeIndex){
                holder.title.setBackgroundColor(Color.parseColor("#4B97F0"));
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (XuantiDialog2.xuantiDialog != null && XuantiDialog2.xuantiDialog.mBaseListenner != null) {
                        //Log.e("test","题目选中下标:\t"+position+"\t"+top3.get(position).getPosition());
                        int p=top3.get(position).getPosition();
                        XuantiDialog2.xuantiDialog.mBaseListenner.onResult(p);
                        XuantiDialog2.xuantiDialog.dismiss();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return top3.size();
        }
    }


}
